package com.zqsoft.dao;

import com.zqsoft.bean.CropBean;
import com.zqsoft.bean.PackageItemBean;
import com.zqsoft.frame.GameMember;
import com.zqsoft.guiHelper.bean.PackageItem;
import com.zqsoft.utils.FileUtils;
import java.util.ArrayList;
import java.util.List;

public class PackageDAO {
	
	FileUtils fileUtils=new FileUtils();
	// 实现获取用户包裹详情列表的方法，接收用户编号作为参数
    public List<PackageItem> getUserPackage(int userId) {
    	CropDAO cropDao=new CropDAO();
        // 定义一个用于存储用户包裹详情对象的集合，初始为空
        List<PackageItem> userPackages = new ArrayList<>();

        // 拼接出用户包裹文件的完整路径
        String filePath = "user/userPack/" + userId + "_package.txt";
        List<String> fileContentLines = fileUtils.readFile(filePath);
        List<CropBean>AllCrop=cropDao.getAllCrop();
        // 遍历返回的List集合
        for (String line : fileContentLines) {
        	CropBean cropBeantmp=null;
            String[] tmp = line.split(":");
            for(CropBean cropBean:AllCrop) {
            	if(cropBean.getCropId()==Integer.parseInt(tmp[0]))
            		cropBeantmp=cropBean;
            };

            PackageItemBean packageItem = new PackageItemBean(cropBeantmp, Integer.parseInt(tmp[1]));
            userPackages.add(packageItem);
        }

        return userPackages;
    }
    
    public static void writePackageFile(int userId, List<PackageItemBean> packageItemBeans) {
    	String filePath = "user/userPack/" + userId + "_package.txt";
    	String str="";
    	for(int i=0;i<packageItemBeans.size();i++) {
    		PackageItemBean packageItem=packageItemBeans.get(i);
    		str +=packageItem.getCropId()+":"+packageItem.getItemCount();
            if (i != packageItemBeans.size() - 1) {
                str += "\n";
            }
    }
        FileUtils.writeFile(filePath, str);
  }  	
}